package view.content;

import java.awt.GridLayout;
import java.util.List;

import model.Player;
import model.Preference;
import model.contents.Content;
import model.inventory.InventoryRecord;
import model.inventory.InventoryTable;
import util.PlayerUtilities;

public class RealEstateListingBox extends ContentBox {
	private InventoryTable tableModel;
	
	public RealEstateListingBox(Content c, int width, int height) {
		super(c, width, height);
		if(c instanceof InventoryTable) {
			this.tableModel = (InventoryTable)c;
		}
		setupLayout(width, height);
		layoutComponents();
		//System.out.println("From realestate box "+c.getType()+" "+c.getLocalPath());
	}
	
	private void layoutComponents() {
		List<InventoryRecord> list = this.tableModel.getInventoryRecords();
		
		System.out.println(" ====== list size "+list.size()+" ====="+tableModel.getNumRows());
		//list.get(1)
		for(int i=0;i<tableModel.getNumRows() && i<list.size();i++) {
			String model = list.get(i).getFieldValue("model");
			String price = list.get(i).getFieldValue("price");
			
			int contentID = Integer.parseInt(list.get(i).getFieldValue("picture1"));
			content = Player.getInstance().getContent(contentID);
			
			//System.out.println("From layout component = "+list.get(i).getFirstImage().getLocalPath());
			RecordSquareView view = new RecordSquareView(model, price, content, width,tableModel.getRowHeight());
			this.add(view);
		}
	}

	private void setupLayout(int width, int height) {
		GridLayout layout = new GridLayout(tableModel.getNumRows(), 1);
		layout.setVgap(this.getVerticalSpacing(height));
		this.setLayout(layout);
	}
	
	private int getVerticalSpacing(int height) {
		if(tableModel.getNumRows()<=1) {
			return 0;
		}
		// System.out.format("%d/%d-%d\n", height, tableModel.getNumRows(), tableModel.getRowHeight());
		return PlayerUtilities.scale((height-tableModel.getNumRows()*tableModel.getRowHeight())/(tableModel.getNumRows()-1), Preference.getInstance().getScale());
	}
	

}
